using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using TinyServerLib;
using TinyLogger;

namespace WinformWebServer
{
    public partial class Form1 : Form
    {
        TinyServer server = new TinyServer();
        
        public Form1()
        {
            InitializeComponent();

            server.ServerLogger.AddLoggerSink(new TextBoxLogger(this.textBox1));
            server.ServerLogger.SetLevel(LogLevel.Informational);
            this.configure(server);
        }



        private void configure(TinyServerLib.TinyServer server)
        {
            server.Templates = System.Configuration.ConfigurationManager.AppSettings["TemplatePath"];
            server.DefaultPage = System.Configuration.ConfigurationManager.AppSettings["Default"];
            server.WebRootPath = System.Configuration.ConfigurationManager.AppSettings["WebRoot"];
            server.Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);
            string logLevel = System.Configuration.ConfigurationManager.AppSettings["LogLevel"].ToUpper();
            switch (logLevel)
            {
                case "ALL": server.ServerLogger.SetLevel(LogLevel.Informational);
                    break;
                case "WARNING": server.ServerLogger.SetLevel(LogLevel.Warning);
                    break;
                case "ERROR": server.ServerLogger.SetLevel(LogLevel.Error);
                    break;
                case "NONE": server.ServerLogger.SetLevel(LogLevel.Fatal);
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            this.button2.Enabled = true;
            this.server.Run();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button2.Enabled = false;
            this.button1.Enabled = true;
            this.server.Stop();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.textBox1.SelectionStart = textBox1.Text.Length;
            this.textBox1.ScrollToCaret();
        }

    }
}